package com.example.accalc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Program extends Activity {
	
	public int leftCalculatorOperand = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Views
        
        final Button buttonClear = (Button) findViewById(R.id.ButtonClear);
        final Button button0 = (Button) findViewById(R.id.Button0);
        final Button button1 = (Button) findViewById(R.id.Button1);
        final Button button2 = (Button) findViewById(R.id.Button2);
        final Button button3 = (Button) findViewById(R.id.Button3);
        final Button button4 = (Button) findViewById(R.id.Button4);
        final Button button5 = (Button) findViewById(R.id.Button5);
        final Button button6 = (Button) findViewById(R.id.Button6);
        final Button button7 = (Button) findViewById(R.id.Button7);
        final Button button8 = (Button) findViewById(R.id.Button8);
        final Button button9 = (Button) findViewById(R.id.Button9);
        final Button buttonDivide = (Button) findViewById(R.id.ButtonDivide);
        final Button buttonMultiply = (Button) findViewById(R.id.ButtonMultiply);
        final Button buttonAdd = (Button) findViewById(R.id.ButtonAdd);
        final Button buttonSubtract = (Button) findViewById(R.id.ButtonSubtract);
        final Button buttonResult = (Button) findViewById(R.id.ButtonResult);
        final EditText CalculateEditorEditText  = (EditText) findViewById(R.id.CalculateEditorEditText);
        
        // Variables
        
        
        
        // Events
        
        buttonClear.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.setText("");
            }
        });
        
        button0.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("0");
            }
        });
        
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("1");
            }
        });
        
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("2");
            }
        });
        
        button3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("3");
            }
        });
        
        button4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("4");
            }
        });
        
        button5.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("5");
            }
        });
        
        button6.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("6");
            }
        });
        
        button7.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("7");
            }
        });
        
        button8.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("8");
            }
        });
        
        button9.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("9");
            }
        });
        
        buttonAdd.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	CalculateEditorEditText.append("+");
            }
        });
        
        buttonResult.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	String calculateEditorString = CalculateEditorEditText.getText().toString().trim();
            	int result = 0;
            	if(!calculateEditorString.isEmpty()){
            		char[] expressionCharArray = calculateEditorString.toCharArray();
            		
            		int arrayIndex = 0;
            		while(expressionCharArray.length > arrayIndex){
            			
            			if(expressionCharArray[arrayIndex] == '+' /*|| 
            					expressionCharArray[arrayIndex] == '-'*/){
            				result = Integer.parseInt(calculateEditorString.substring(0, arrayIndex)) +
            				Integer.parseInt(calculateEditorString.substring( arrayIndex + 1, calculateEditorString.length()));
            				
            				CalculateEditorEditText.setText(Integer.toString(result));
            				
            				break;
            			}
            			
            			arrayIndex++;
            		}
            	}
            }
        });
    }
}